package com.ddh.codeduck.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.codeduck.pojo.dto.CodeDuckTagDTO;
import com.codeduck.pojo.entity.CodeDuckTag;
import com.codeduck.pojo.req.CodeDuckTagPageReq;
import com.ddh.codeduck.service.ICodeDuckTagService;
import com.codeduck.pojo.vo.CodeDuckTagVO;
import com.ddhcodeduck.common.result.BaseResponse;
import com.ddhcodeduck.common.result.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author ddh
 * @since 2025-07-03
 */
@RestController
@RequestMapping("/codeDuckTag")
@Api(tags = "标签表")
public class CodeDuckTagController {

    @Resource
    private ICodeDuckTagService codeDuckTagService;

    /**
     * 查询所有标签并且分页
     *
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询所有标签-分页")
    public BaseResponse<IPage<CodeDuckTagVO>> list(@RequestBody CodeDuckTagPageReq codeDuckTagPageReq) {
        IPage<CodeDuckTagVO> codeDuckTagVOIPage = codeDuckTagService.listTagList(codeDuckTagPageReq);
        return ResultUtils.success(codeDuckTagVOIPage);
    }

    /**
     * 查询所有二级标签
     */
    @PostMapping("/listSecondary")
    @ApiOperation(value = "查询所有二级标签，我感觉你们是用不到这个接口，我先写着")
    public BaseResponse<List<CodeDuckTagVO>> listSecondary() {
        List<CodeDuckTag> codeDuckTagList = codeDuckTagService.list();
        List<CodeDuckTagVO> codeDuckTagVOList = codeDuckTagList.stream()
                .filter(tag -> StringUtils.isNotEmpty(tag.getParentId()))
                .map(tag -> {
                    CodeDuckTagVO vo = new CodeDuckTagVO();
                    BeanUtils.copyProperties(tag, vo);
                    return vo;
                })
                .collect(Collectors.toList());
        return ResultUtils.success(codeDuckTagVOList);
    }

    /**
     * 新增或者修改标签
     */
    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "新增或者修改标签")
    public BaseResponse<Boolean> saveOrUpdate(@RequestBody CodeDuckTagDTO codeDuckTagDTO) {
        Boolean result = codeDuckTagService.saveOrUpdateTag(codeDuckTagDTO);
        return ResultUtils.success(result);
    }

    /**
     * 根据id查询标签
     */
    @PostMapping("/getById")
    @ApiOperation(value = "根据id查询标签")
    public BaseResponse<CodeDuckTag> getById(@RequestParam("id") String id) {
        CodeDuckTag codeDuckTag = codeDuckTagService.getById(id);
        return ResultUtils.success(codeDuckTag);
    }

    /**
     * 批量删除 如果该标签关联题目分类，则不能删除
     */
    @PostMapping("/removeBatchByIds")
    @ApiOperation(value = "批量删除")
    public BaseResponse<Boolean> removeBatchByIds(@RequestBody List<String> ids) {
        Boolean result = codeDuckTagService.removeBatchByIdsService(ids);
        return ResultUtils.success(result);
    }

    /**
     * 根据标签查询题目分类
     */
    @PostMapping("/listQuestionCategory")
    @ApiOperation(value = "根据标签id查询题目分类")
    public BaseResponse<CodeDuckTagVO> listQuestionCategory(@RequestBody CodeDuckTagDTO codeDuckTagDTO) {
        CodeDuckTagVO codeDuckTagVO = codeDuckTagService.listQuestionCategory(codeDuckTagDTO);
        return ResultUtils.success(codeDuckTagVO);
    }

    /**
     * 查询所有标签及其标签下的题目分类
     */
    @GetMapping("/listAndQuestionCategory")
    @ApiOperation(value = "查询所有标签及其标签下的题目分类")
    public BaseResponse<List<CodeDuckTagVO>> listAndQeustionCategory() {
        List<CodeDuckTagVO> codeDuckTagVOList = codeDuckTagService.listAndQuestionCategory();
        return ResultUtils.success(codeDuckTagVOList);
    }

}
